/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.*;
import java.io.IOException;
import javax.swing.tree.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author Ter Murga Victor
 */
public abstract class Utility {
    public static final int thickness = 3;
    public static final Font font = new Font("Arial", Font.BOLD, 15);

    public static String getCommentaire(Node node){
        //String result = null;
        if(node.getAttributes().getNamedItem("commentaire")!=null){
            String commentaire = node.getAttributes().getNamedItem("commentaire").toString();
            commentaire = commentaire.substring(commentaire.indexOf("\"")+1, commentaire.lastIndexOf("\""));
            if(!commentaire.equals("") && !commentaire.equals(" ")){
                return commentaire;
            }
        }
        return null;
    }

    /*public static void getCommentaire(Element commentaire, DefaultMutableTreeNode composante){
        NodeList listEvaluations = commentaire.getElementsByTagName("commentaire");
        System.out.println("evaluation: "+listEvaluations.getLength());
        //if (listEvaluations.getLength()!=0){
            for (int i=0; i<listEvaluations.getLength(); i++){
                Element evaluation = (Element) listEvaluations.item(i);
                String date = evaluation.getAttribute("date");
                String source = evaluation.getAttribute("source");
                String texte = evaluation.getTextContent();
                DefaultMutableTreeNode eval = new DefaultMutableTreeNode(date+": "+source+": "+texte);
                composante.add(eval);
            }
        //}
    }*/

    public static void getEvaluation(Element valeur, DefaultMutableTreeNode composante, String name){
        NodeList listEvaluations = valeur.getElementsByTagName(name);
        if (listEvaluations.getLength()!=0){
            DefaultMutableTreeNode tmp = new DefaultMutableTreeNode(name);
            for (int i=0; i<listEvaluations.getLength(); i++){
                Element evaluation = (Element) listEvaluations.item(i);
                String date = evaluation.getAttribute("date");
                String source = evaluation.getAttribute("source");
                String texte = evaluation.getTextContent();
                DefaultMutableTreeNode eval = new DefaultMutableTreeNode(date+": "+source+": "+texte);
                tmp.add(eval);
            }
            composante.add(tmp);
        }
    }



    public static DefaultMutableTreeNode traiterComposante(Node composante) {

        String nomComposante0 = composante.getAttributes().getNamedItem("nom").toString().replace("nom", "composante").replace("=", " ");
        CheckBoxNode nomComposante = new CheckBoxNode(nomComposante0,true);
        //str.toString().replace("=", "  ");

        //rÃ©cupÃ©rer les commentaires pour ce brique s'ils existent
        /*if (getCommentaire(composante)!=null){
            nomComposante+= " ("+getCommentaire(composante)+") ";
        }*/
       // System.out.println("biennnn >>>>>>>>"+composante.getChildNodes().getLength());
        
        Node sousComposante = composante.getFirstChild();
        boolean valeur=false;

        while(!sousComposante.equals(composante.getLastChild())){
             if (sousComposante.getNodeName().equals("valeur"))
                valeur=true;
             sousComposante = sousComposante.getNextSibling();
        }
       
        DefaultMutableTreeNode nodeComposante;
        if(!valeur){
             nodeComposante = new DefaultMutableTreeNode(nomComposante0);
        }else{
             nodeComposante = new DefaultMutableTreeNode(nomComposante);
        }

        sousComposante = composante.getFirstChild();

         //System.out.print(">> ni<<>>" sousComposante.getNodeName());
        while(!sousComposante.equals(composante.getLastChild())){
        //System.out.println("--<<<"+sousComposant.getNodeName());
            if (sousComposante.getNodeName().equals("commentaires")){
                /*DefaultMutableTreeNode commentaire = new DefaultMutableTreeNode("commentaire");
                getCommentaire((Element) sousComposante, commentaire);
                nodeComposante.add(commentaire);*/
                getEvaluation((Element) sousComposante, nodeComposante, "commentaire");
            }else if (sousComposante.getNodeName().equals("valeur")){
                //String value ="valeur : "+sousComposante.getTextContent();
                //DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(value);
                //nodeComposante.add(nodeValue);
                //DefaultMutableTreeNode value = new DefaultMutableTreeNode("valeur");
                getEvaluation((Element) sousComposante, nodeComposante, "evaluation");
                //getEvaluation((Element) sousComposante, value);
                //nodeComposante.add(value);
            } else if(sousComposante.hasAttributes()){
                DefaultMutableTreeNode nodeSousComposante = traiterSousComposante((Element) sousComposante);
                nodeComposante.add(nodeSousComposante);
                //System.out.println("-------");
            }
            sousComposante = sousComposante.getNextSibling();
        }
        return nodeComposante;
    }

    private static DefaultMutableTreeNode traiterSousComposante(Element sousComposante){
        String nomSousComposante0 = "sous composante: "+sousComposante.getAttribute("nom");
        CheckBoxNode nomSousComposante = new CheckBoxNode(nomSousComposante0,true);

        DefaultMutableTreeNode nodeSousComposante = new DefaultMutableTreeNode(nomSousComposante);

        if(sousComposante.getElementsByTagName("sous_composante").getLength()==0){
            NodeList listValue = sousComposante.getElementsByTagName("valeur");
            for(int i=0; i<listValue.getLength(); i++){
                //String value ="valeur : "+listValue.item(i).getTextContent();
                //DefaultMutableTreeNode value = new DefaultMutableTreeNode("valeur");
                getEvaluation((Element) sousComposante, nodeSousComposante, "evaluation");
                //nodeSousComposante.add(value);
            }
            //System.out.println("compo: "+sousComposante.getAttribute("nom")+" pas de sous");
        } else {
            NodeList listSousCompo = sousComposante.getElementsByTagName("sous_composante");
            //System.out.println("-->>"+sousComposante.getChildNodes().getLength());
            for (int i=0; i<listSousCompo.getLength(); i++){
                Element tmp = (Element) listSousCompo.item(i);
                if(tmp.getParentNode().equals(sousComposante)){
                //System.out.println("i="+i+" / tmp="+tmp.getAttribute("nom")+" / nbLignes="+nbLignes+" / gauche="+gauche);
                    DefaultMutableTreeNode nodeTmp =traiterSousComposante(tmp);
                    nodeSousComposante.add(nodeTmp);
                }
            }
        }
        return nodeSousComposante;
    }



    public static DefaultMutableTreeNode createTree(NodeList listArbreDesComposantes, String nom){
        DefaultMutableTreeNode tree = new DefaultMutableTreeNode(nom,true);

        for (int i=0; i<listArbreDesComposantes.getLength();i++){
            //Recuperer l'arbre des composantes
            Element arbre = (Element) listArbreDesComposantes.item(i);

            // Recuperer la liste des composantes
            NodeList listComposant = arbre.getElementsByTagName("composante");

            for (int j=0; j<listComposant.getLength(); j++){

                //Element composant = (Element) listComposant.item(j);
                Node composant = listComposant.item(j);

                DefaultMutableTreeNode tmp =Utility.traiterComposante(composant);

                tree.add(tmp);
            }
        }

        /*JTree result = new JTree(tree);

        result.setUI(new BasicTreeUI(){
            @Override
            protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds,
                        TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)  { }
            @Override
            protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path)  { }
        });

        ((BasicTreeUI) result.getUI()).setExpandedIcon(null);
        ((BasicTreeUI) result.getUI()).setCollapsedIcon(null);

        ImageIcon leafIcon = new ImageIcon(groupe.App.class.getResource("resources/crayon.png"));
        if (leafIcon != null) {
            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
            renderer.setLeafIcon(leafIcon);
            result.setCellRenderer(renderer);
        } else {
            System.err.println("Leaf icon missing; using default.");
        }

        result.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);*/


        //scrollPane.setPreferredSize(new Dimension(200, 200));
        //scrollPane.setHorizontalScrollBar(null);
        //scrollPane.setViewportBorder(new LineBorder(Color.GREEN));
        //scrollPane.setForeground(Color.red);
        return tree;
    }
    public static JTree montrer(DefaultMutableTreeNode debut, String nom){

        JTree result = new JTree(debut);
        //result.set("STRUCTURE");

      /*  result.setUI(new BasicTreeUI(){
            @Override
            protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds,
                        TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf)  { }
            @Override
            protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, Insets insets, TreePath path)  { }
        });
*/
       // ((BasicTreeUI) result.getUI()).setExpandedIcon(null);
      //  ((BasicTreeUI) result.getUI()).setCollapsedIcon(null);


   /*     ImageIcon leafIcon = new ImageIcon(groupe.App.class.getResource("resources/crayon.png"));
        if (leafIcon != null) {
            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
            renderer.setLeafIcon(leafIcon);
            result.setCellRenderer(renderer);
        } else {
            System.err.println("Leaf icon missing; using default.");
        }
*/
        CheckBoxNodeRenderer renderer1 = new CheckBoxNodeRenderer();
        //CheckboxCellRenderer_1 renderer1 = new CheckboxCellRenderer_1();

        result.setCellRenderer(renderer1);
        result.setCellEditor(new CheckBoxNodeEditor(result));
     //   result.setCellEditor(new CheckboxNodeEditor_1());
        result.setEditable(true);
        //result.setShowsRootHandles(true);
        result.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        /*JCheckBox check =new JCheckBox();
        JLabel label=new JLabel();

        result.setLayout(null);
        result.add(check = new JCheckBox(check.getText()));
        result.add(label = new JLabel(label.getName()));
*/
       // result.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);


        //scrollPane.setPreferredSize(new Dimension(200, 200));
        //scrollPane.setHorizontalScrollBar(null);
        //scrollPane.setViewportBorder(new LineBorder(Color.GREEN));
        //scrollPane.setForeground(Color.red);
        return result;
    }


}
